<?php
require(dirname(__FILE__)."/includes/init.php");
if(isset($_SESSION['user_name'])){
$pg = isset($_REQUEST['pg'])?$_REQUEST['pg']:'1';
$pageNum = 10;
$nextPage = $pageNum * ($pg-1);

$sql = "select id,title,type,brownserNum,timestamp from template order by id desc limit $nextPage,$pageNum";
$row = $GLOBALS['db']->dbfetchAll($sql);

$sql = "select templateId,count(*) as clickNum from clickTrack group by templateId;";
$rows = $GLOBALS['db']->dbfetchAll($sql);

foreach($row as $key=>$value){
	foreach($rows as $k=>$v){
		if(empty($v['templateId'])){
			$v['templateId'] = 1;
		}
		if($value['id']==$v['templateId']){
			$row[$key]['clickNum']=$v['clickNum'];
		}
	}
	if(!array_key_exists("clickNum",$row)){
		$row[$key]['clickNum'] = 0;
	}
}
?>
<title>模板列表</title>
<table width="600" border="0" style="background:#FFFFFF">
  <tbody><tr>
    <td>Wish you a Happy New Year!</td>
    <td></td>
  </tr>
  <tr>
    <td>ID</td>
    <td>TITLE</td>
    <td>TYPE</td>
    <td>Num</td>
    <td>SHIJIAN</td>
    <td>check</td>
    <td>Edit</td>
  </tr>  
<?php foreach ($row as $key => $value) {?>
  <tr>
    <td><?php echo $value['id'];?></td>
    <td><?php echo $value['title'];?></td>
    <td><?php echo $value['type'];?></td>
    <td><?php echo $value['brownserNum'];?></td>
    <td><?php echo date("Y-m-d H:i:s",$value['timestamp']);?></td>
    <td><a href="check.php?id=<?php echo $value['id'];?>" target="_blank">check</a></td>
    <td><a href="edit.php?id=<?php echo $value['id'];?>" target="_blank">Edit</a></td>
  </tr>
<?php } ?>
  <tr>
    <td>&nbsp;</td>
    <td colspan="5" style="font-size:12px"><?php
	 $sql = "select count(*) from template";
	 $totalNum = $GLOBALS['db']->dbGetRow($sql);
	 $page = new Pager($totalNum[0],$pageNum,$style="page");?></td>
    <td>&nbsp;</td>
  </tr>  
</tbody></table>
<?php }
else{
	exit;
}
?>
